/** ChickenWarrior, a Simple Java ME game developed in Gnu/Linux
  *  Copyright (C) <2008>  Maxin B. John <maxinbjohn@gmail.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 3 of the License, or
  *  any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, see http://www.gnu.org/licenses
  *  or write to the Free Software Foundation,Inc., 51 Franklin Street,
  *  Fifth Floor, Boston, MA 02110-1301  USA
  * 
  */

package com;

import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.Random;

public class EggFall implements Runnable,CommandListener{

    private static final int SPEED = 3;
    private MyGameCanvas canvas;
    private Sprite sprite;
    
    private byte previousDirection = GameCanvas.DOWN;
    private byte direction = GameCanvas.DOWN;
    private boolean interrupted;
    private int[] downSeq;
    private int downTrans;
    private int[] upSeq;
    private int upTrans;
    private int[] leftSeq;
    private int leftTrans;
    private int[] rightSeq;
    private int rightTrans;
    private int finalScore=0;
    private Command exitCommand;
    private Form form;
    private StringItem stringItem;
    private Display dsp;
    
 
    public EggFall(MyGameCanvas canvas, Sprite sprite) {
        this.canvas = canvas;
        this.sprite = sprite;
    }
   
    /////////////////////////////////////////////////////////
    public void commandAction(Command command, Displayable displayable){
         
   if (command == exitCommand) {
             exitMIDlet();
             System.out.println("Exit Command \n");  
            }
    
      }
    
    public Command getExitCommand() {
       
        if (exitCommand == null) {
           exitCommand = new Command("Exit", Command.EXIT, 0);
           }
        return exitCommand;
    }
    

      public void exitMIDlet() {
            this.canvas.mid.notifyDestroyed();
     
    }
      
     

      
     /////////////////////////////////////////////////////////
    public void setSequences(int[] downSeq, int downTrans, int[] upSeq, int upTrans, int[] leftSeq, int leftTrans, int[] rightSeq, int rightTrans) {
        this.downSeq = downSeq;
        this.downTrans = downTrans;
        this.upSeq = upSeq;
        this.upTrans = upTrans;
        this.leftSeq = leftSeq;
        this.leftTrans = leftTrans;
        this.rightSeq = rightSeq;
        this.rightTrans = rightTrans;
    }

    public void stop() {
        this.interrupted = true;
    }
    
   public int getRandomNumber(){
        int intervalMin = 10;
        int intervalMax = 100;
        int halfInterval = (intervalMin - intervalMax) / 2;
        int integerDisc = Integer.MAX_VALUE / halfInterval;
        Random r = new Random(System.currentTimeMillis());
        int randomNumber = r.nextInt();
        randomNumber = randomNumber / integerDisc;
        randomNumber = randomNumber + halfInterval;
        randomNumber = randomNumber <0 ? -1*randomNumber: randomNumber;
        
        return randomNumber;
    }
    
    public void run() {
        while (!this.interrupted) {
            if (this.direction == GameCanvas.DOWN) {
                if (this.previousDirection != this.direction) {
                    this.sprite.setFrameSequence(this.downSeq);
                    this.sprite.setTransform(this.downTrans);
                    this.previousDirection = this.direction;
                }
                this.sprite.move(0, SPEED);
                if(this.sprite.getY()> this.canvas.canvasHeight && this.sprite.isVisible()){
                    System.out.println("This egg got broken .. you loose one egg\n");
                    this.sprite.setVisible(false);
                    if (this.canvas.getEggNo() >= 10){
                        System.out.println("Game over You scored ..\n"+ this.finalScore);
                        this.canvas.stop();
                        this.stop();
                        this.canvas.setFullScreenMode(false);
                        this.canvas.setCommandListener(this);
                        this.canvas.addCommand(this.getExitCommand());
                        
                     }
                    else{
                        this.canvas.setEggNo();
                        this.sprite.setPosition(this.getRandomNumber(), 0);
                        this.sprite.setVisible(true);
                         System.out.println(this.canvas.getEggNo());
                    }
                    }
                
                if (this.canvas.spriteCollides(this.sprite)) {
                    this.sprite.setVisible(false);
                    this.finalScore += 1;
                    System.out.println("You secured your egg!scored"+this.finalScore+"\n");
                    
                     if (this.canvas.getEggNo() >= 10){
                        System.out.println("Game over \n"+ this.finalScore);
                        this.canvas.stop();
                        this.stop();
                        this.canvas.setFullScreenMode(false);
                        this.canvas.setCommandListener(this);
                        this.canvas.addCommand(this.getExitCommand());
                    }
                    else{
                        this.canvas.setEggNo();
                        this.sprite.setPosition(this.getRandomNumber(), 0);
                        this.sprite.setVisible(true);
                         System.out.println(this.canvas.getEggNo());
                    }
                    continue;
                }
             }
            try {
                Thread.sleep(200);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
}
